<template>
    <div>
        <el-button type="primary" plain @click="add">添加</el-button>
        <el-table
            :data="goodslist"
            row-key="id"
            border
            style="width: 100%"
            :tree-props="{ children: 'children' }"
        >
            <el-table-column prop="id" label="商品编号"></el-table-column>
            <el-table-column prop="goodsname" label="商品名称" width="160">
            </el-table-column>
            <el-table-column prop="price" label="商品价格" width="160">
            </el-table-column>
            <el-table-column prop="market_price" label="市场价格" width="160">
            </el-table-column>
            <el-table-column label="图片">
                <template slot-scope="item">
                    <el-image
                        v-if="item.row.img"
                        style="width: 100%"
                        :src="$host + item.row.img"
                        fit="fill"
                    ></el-image>
                </template>
            </el-table-column>
            <el-table-column label="是否热卖">
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.ishot"
                        active-color="#13ce66"
                        :active-value="1"
                        :inactive-value="2"
                        @change="setgoods(scope.row)"
                    >
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column label="是否新品">
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.isnew"
                        active-color="#13ce66"
                        :active-value="1"
                        :inactive-value="2"
                        @change="setgoods(scope.row)"
                    ></el-switch>
                </template>
            </el-table-column>
            <el-table-column label="状态">
                <template slot-scope="scope">
                    <el-tag type="success" plain size="middle">{{
                        scope.row.status == 1 ? "启用" : "禁用"
                    }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作" ref="goods">
                <template slot-scope="scope">
                    <el-button
                        type="primary"
                        icon="el-icon-edit"
                        circle
                        @click="edit(scope.row)"
                    ></el-button>
                    <el-button
                        type="danger"
                        icon="el-icon-delete"
                        circle
                        @click="del(scope.row.id)"
                    ></el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page.page"
            :page-size="page.size"
            layout="prev, pager, next, jumper,total"
            :total="page.total"
        >
        </el-pagination>
    </div>
</template>

<script>
//引入vuex
import Vuex from "vuex";
export default {
    data() {
        return {};
    },
    methods: {
        //引入 商品管理列表 接口
        ...Vuex.mapActions({
            getlist: "goods/goodslist_a",
            goodsdel: "goods/goodsdelete_a",
            goodsinfo: "goods/goodsinfo_a",
            goodscount: "goods/goodscount_a",
            goodsedit: "goods/goodsedit_a",
        }),
        //引入 商品管理设置
        ...Vuex.mapMutations({
            setgoodscount: "goods/setgoodscount",
        }),
        //设置是否是热卖,设置是否是新品
        setgoods(row) {
            // // 这是传scope.row的方法，需要删除三个内容
            // let newInfo = JSON.parse(JSON.stringify(row));
            // //必须删除这三个内容，否则后台接收错误
            // delete newInfo.firstcatename;
            // delete newInfo.secondcatename;
            // delete newInfo.number;

            let newInfo = { id: row.id, ishot: row.ishot, isnew: row.isnew };
            //必须要传一个formdata格式
            let fd = new FormData();
            for (var key in newInfo) {
                fd.append(key, newInfo[key]);
            }
            this.goodsedit(fd);
        },
        //点击添加
        add() {
            this.$emit("changeBtn", true);
            //把修改vuex上的值，dialog监听 vuex数据变化就给上
            this.$store.commit("goods/seteditor", "");
        },
        //点击编辑
        edit(row) {
            this.$emit("changeBtn", false); //点击编辑 不是添加按钮
            this.goodsinfo(row.id);
            //如果有img图片，那么上传给fileList文件列表
            if (row.img) {
                //把fileList的数据变成img,使得点击编辑的时候可以查看到图片
                this.$store.commit("cate/setfileList", [
                    { name: row.goodsname, url: this.$host + row.img },
                ]);
            }
            //把描述给到vuex上，dialog监听 vuex数据变化就给上
            this.$store.commit("goods/seteditor", row.description);
        },
        //点击删除
        del(id) {
            //点击弹出
            this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    this.goodsdel(id);
                    //判断当时如果页码等于总页码 而且 只有一条数据时候 回退一页页面
                    if (
                        this.page.page == this.page.totalPage &&
                        this.goodslist.length == 1
                    ) {
                        this.page.page -= 1;
                    }
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除",
                    });
                });
        },
        //分页器的函数
        //修改当前查询的条数
        handleSizeChange() {},
        //修改当前页码数
        handleCurrentChange(val) {
            this.page.page = val;
            // this.setgoodscount({page:val})
            this.getlist(this.page);
        },
    },
    mounted() {
        //调用
        this.getlist(this.page);
        this.goodscount();
    },
    computed: {
        //引入 商品管理列表数据
        ...Vuex.mapGetters({
            goodslist: "goods/getgoodslist",
            page: "goods/getPage",
        }),
    },
};
</script>

<style scoped>
.el-pagination {
    float: right;
}
/deep/ .el-pagination__total {
    margin-left: 20px;
}
</style>
